C语言程序找错`

来源:百度知道 编辑:UC知道 时间:2024/05/09 12:53:45
这个程序目的是统计不大于100的负数,
当输入负数的时候,
提示是否继续输入数字
可是这个程序有问题,
帮忙找一下错误.
1、输入负数后提示完输入y or n,会提示输入错误
2、一开始输入a,b,c这样的字母会一直循环下去
#include "stdio.h"
main()
{
int m,count=0;
char n;
while (1)
{
printf("Please input an integer:");
scanf("%d",&m);
if (m>=0 && m<=100)
count++;
else if (m>100)
;
else if (m<0)
{
printf("continue?Y&N:");
scanf("%d",&n);
if (n=='Y')
continue;
else if (n=='N')
{
printf("%d",count);
break;
}
else
{
printf("Input Error!");
break;
}
}
else
{
printf("Input error!");
break;
}
}
}

#include "stdio.h"

main()
{
int m,count=0;
char h;
while(1)
{
printf("Please input an integer:");
scanf("%d",&m);
if (m>=0 && m<=100)
count++;
else if (m>100)
count=count;
else if(m<0)
{
char j;
printf("continue?Y&N:");
scanf("%s",&h);

switch(h)
{
case 'Y':
case 'y': break;
case 'N':
case 'n': printf("count=%d\n",count); return;
default: printf("Input Error!");break;
}
}
}
}

我帮你改好啦,大小写的Y、N都可以啦。应该用%s接收输入才行的,%c也会出错的。
不过你的第2个问题是无法解决的,因为你在输入时设定了scanf("%d",&m);,输入的一定要是数字,那你输入了字母就肯定会错了。

改下下面两处注释就行了,还要记得输入的要是大写Y或N
#include "stdio.h"
main()
{
int m,count=0;
char n;
while (1)
{
pr